<template>
	<xn-form-container
		:title="formData.id ? '编辑鸡群' : '增加鸡群'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="鸡群类别：" name="category">
				<a-select v-model:value="formData.category" placeholder="请选择鸡群类别" :options="categoryOptions"
						  @change="handleCategoryChange" allow-clear/>
			</a-form-item>
			<a-form-item label="鸡群种类：" name="type">
				<a-select v-model:value="formData.type" placeholder="请选择鸡群种类" :options="typeOptions"
						  :disabled="formData.category === undefined" allow-clear/>
			</a-form-item>
			<a-form-item label="所属栏区：" name="columnId">
				<columnTreeSelect ref="columnTreeSelectRef" @update:columnId="handleColumnIdUpdate"/>
			</a-form-item>
			<a-form-item label="鸡群数量：" name="quantity">
				<a-input v-model:value="formData.quantity" placeholder="请输入鸡群数量" allow-clear />
			</a-form-item>
			<a-form-item label="入栏日期：" name="arrivalDate">
				<a-date-picker v-model:value="formData.arrivalDate" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择入栏日期" style="width: 100%" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizChickenFlockForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizChickenFlockApi from '@/api/chickenflock/bizChickenFlockApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const columnTreeSelectRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const categoryOptions = ref([])
	const typeOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		categoryOptions.value = tool.dictList('column_sign')

	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		category:[required('请选择鸡群类别')],
	    type:[required('请选择鸡群类型')],
		arrivalDate:[required('请输入入栏日期')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			bizChickenFlockApi
				.bizChickenFlockSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	const handleCategoryChange = value =>{
		clearChickenType()
		typeOptions.value = value === '肉鸡' ? tool.dictList('broiler_type') : tool.dictList('layer_type');
		if (value === undefined){
			clearChickenType();
		}
	}

	const clearChickenType = () =>{
		formData.value.type = undefined;
		typeOptions.value = [];
	}

	const handleColumnIdUpdate = (value) =>{
		formData.value.columnId = value;
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
